
<div class="span9">
            <h1 class="page-title">新闻列表</h1>
<div class="btn-toolbar">
    <a href="index.php?c=cate&a=add&admin=1" class="btn btn-primary"><i class="icon-plus"></i> 新闻添加</a>
    <button class="btn">Import</button>
    <button class="btn">Export</button>
  <div class="btn-group">
  </div>
</div>
<div class="well">
            <table class="table">
                <thead>
                    <tr>
                      <th>id</th>
                      <th>名字</th>
                      <th>pid</th>

                    </tr>
                </thead>
                <tbody>
                    <?php foreach($cate as $v){ ?>
                        <tr pid="<?php echo $v['pid'];?>" level="1">
                            <td filed="id"> <?php echo $v['id'];?> </td>
                            <td filed="title" class="edit" ><span class="label label-info">+</span> <?php echo $v['name'];?> </td>
                            <td filed="type" class="edit"> <?php echo $v['pid'];?> </td>
                            <td> <a href="index.php?c=cate&a=edit&admin=1&id=<?php echo $v['id'];?>">编辑<i class="icon-pencil"></i></a> </td>
                            <td> <a href="index.php?c=cate&a=del&admin=1&id=<?php echo $v['id'];?>" class="del" role="button" onclick="return confirm('请确认是否真的要删除？')">删除<i class="icon-remove"></i></a> </td>

                        </tr>
                    <?php } ?>


                </tbody>

            </table>

        </div>

<script src="resources/admin/js/jQuery.js" type="text/javascript"></script>
<script type="text/javascript">

$(function(){

    $('tr').on('click','.del',function() {

        var url = $(this).attr('href');

        $.ajax({
            url: url,
            type: 'GET',

            success:function(json){

                if(json>0){
                    if(confirm('该分类还有子分类，真的要全部删除吗？')){
                        location.href=url;
                    }
                }else{
                    location.href=url;
                }

            }
        })
        return false;
    });

    $('tbody').on('click','.label',function(){

        if($(this).text() == '+') {

            var tr_obj = $(this).parent().parent();

            var this_pid = tr_obj.attr('pid');//所点击的当条tr标签的pid属性值

            var len = $('tr[pid='+this_pid+']').length;

            var index = $('tr[pid='+this_pid+']').index(tr_obj);

            var pid = tr_obj.find('td:first').text();//所点击当条记录的ID

            var level = tr_obj.attr('level');
            var offset = level*40;
            level++;

            $(this).text('-');
            $.ajax({
                url: 'index.php?c=cate&a=list&admin=1',
                type: 'POST',
                dataType: 'json',
                data: {pid: pid},
                success:function(json){

                    var html = '';
                    for(var i in json){
                        html+='<tr pid="'+json[i].pid+'" level="'+level+'">';
                        html+='<td filed="id">'+json[i].id+'</td>';
                        html+='<td filed="title" class="edit"><span class="label label-info" style="margin-left:'+offset+'px;">+</span>'+json[i].name+'</td>';
                        html+='<td filed="type" class="edit">'+json[i].pid+'</td>';
                        html+='<td> <a href="index.php?c=cate&a=edit&admin=1&id='+json[i].id+'">编辑<i class="icon-pencil"></i></a> </td>';
                        html+='<td> <a href="index.php?c=cate&a=del&admin=1&id='+json[i].id+'" class="del" role="button" onclick="return confirm('+ "'请确认是否真的要删除？'"+')">删除<i class="icon-remove"></i></a> </td>';

                        html+='</tr>';
                    }

                    if (index == len-1) {
                        html+='<tr pid="'+this_pid+'"></tr>';

                    };
                    //console.log(html);

                    tr_obj.after(html);
                }
            })
        }else {

            $(this).text('+');
            var tr_obj = $(this).parent().parent();
            var pid = tr_obj.attr('pid');

            tr_obj.nextUntil('tr[pid='+pid+']').remove();


        }


    })

})



</script>